package com.castle.fortress.admin.ai.service.impl;

import cn.hutool.core.util.StrUtil;

import com.castle.fortress.common.utils.ConvertUtil;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.ai.entity.AiRecordEntity;
import com.castle.fortress.admin.ai.dto.AiRecordDto;
import com.castle.fortress.admin.ai.mapper.AiRecordMapper;
import com.castle.fortress.admin.ai.service.AiRecordService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import java.util.List;

/**
 * ai信息记录表 服务实现类
 *
 * @author 
 * @since 2024-03-09
 */
@Service
public class AiRecordServiceImpl extends ServiceImpl<AiRecordMapper, AiRecordEntity> implements AiRecordService {
    /**
    * 获取查询条件
    * @param aiRecordDto
    * @return
    */
    public QueryWrapper<AiRecordEntity> getWrapper(AiRecordDto aiRecordDto){
        QueryWrapper<AiRecordEntity> wrapper= new QueryWrapper();
        if(aiRecordDto != null){
            AiRecordEntity aiRecordEntity = ConvertUtil.transformObj(aiRecordDto,AiRecordEntity.class);
            wrapper.like(aiRecordEntity.getId() != null,"id",aiRecordEntity.getId());
            wrapper.like(aiRecordEntity.getUserId() != null,"user_id",aiRecordEntity.getUserId());
            wrapper.like(aiRecordEntity.getGroupId() != null,"group_id",aiRecordEntity.getGroupId());
        }
        wrapper.orderByDesc("create_time");
        return wrapper;
    }


    @Override
    public IPage<AiRecordDto> pageAiRecord(Page<AiRecordDto> page, AiRecordDto aiRecordDto) {
        QueryWrapper<AiRecordEntity> wrapper = getWrapper(aiRecordDto);
        Page<AiRecordEntity> pageEntity = new Page(page.getCurrent(), page.getSize());
        Page<AiRecordEntity> aiRecordPage=baseMapper.selectPage(pageEntity,wrapper);
        Page<AiRecordDto> pageDto = new Page(aiRecordPage.getCurrent(), aiRecordPage.getSize(),aiRecordPage.getTotal());
        pageDto.setRecords(ConvertUtil.transformObjList(aiRecordPage.getRecords(),AiRecordDto.class));
        return pageDto;
    }


    @Override
    public List<AiRecordDto> listAiRecord(AiRecordDto aiRecordDto){
        QueryWrapper<AiRecordEntity> wrapper = getWrapper(aiRecordDto);
        List<AiRecordEntity> list = baseMapper.selectList(wrapper);
        return ConvertUtil.transformObjList(list,AiRecordDto.class);
    }

    @Override
    public List<AiRecordEntity> getByTopN(String groupId, String userId) {

        return baseMapper.getByTopN(groupId,userId);
    }
}

